import React from 'react';
import { Link, withRouter } from 'react-router'

class InuLink extends React.Component {
    constructor(props) {
        super(props);
        this.displayName = 'InuLink';
        this.state = {}
    }
    render() {
        const to = InuLink.makeTo({ ...this.props.location }, this.props.to, this.props.query)
        return (
            <Link activeStyle={ this.props.activeStyle }
                  activeClassName={ this.props.activeClassName }
                  onlyActiveOnIndex={ this.props.onlyActiveOnIndex }
                  onClick={ this.props.onClick }
                  target={ this.props.target }
                  to={ to }>
            { this.props.children }
            </Link>
        )
    }
}

InuLink.makeTo = function(location, to, query) {
    if (typeof to == "string")
        location.pathname = to
    else if (typeof to == "object")
        Object.assign(location, to)
    if (typeof query == "object")
        Object.assign(location.query, query)
    return location
}

InuLink.defaultProps = {
    to: {}
}

export default withRouter(InuLink)
